package top.xbaoziplus.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import top.xbaoziplus.company.empty.Position;
import top.xbaoziplus.company.mapper.PositionMapper;
import top.xbaoziplus.company.vo.ApplyVo;
import top.xbaoziplus.company.common.Result;
import top.xbaoziplus.company.dto.ApplyDto;
import top.xbaoziplus.company.empty.Apply;
import top.xbaoziplus.company.empty.Audit;
import top.xbaoziplus.company.empty.Staff;
import top.xbaoziplus.company.mapper.AuditMapper;
import top.xbaoziplus.company.mapper.StaffMapper;
import top.xbaoziplus.company.service.ApplyService;
import top.xbaoziplus.company.mapper.ApplyMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply>
        implements ApplyService {

    @Autowired
    private ApplyMapper applyMapper;

    @Autowired
    private AuditMapper auditMapper;

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private PositionMapper positionMapper;

    /**
     * @description 用户提交申请
     * @author kang
     * @date 2022/7/1 上午 10:24
     */
    @Override
    public Result addApply(ApplyVo applyVo) {
        if (ObjectUtils.isEmpty(applyVo.getContent())
                && ObjectUtils.isEmpty(applyVo.getType())
                && ObjectUtils.isEmpty(applyVo.getReason())) {
            return Result.fail("参数错误");
        }
        //判断当前是否存在申请
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Apply::getJobId, applyVo.getJobId())
                .eq(Apply::getType, applyVo.getType())
                .eq(Apply::getStatus, 0);
        Apply apply = applyMapper.selectOne(queryWrapper);
        if (!ObjectUtils.isNull(apply)) {
            return Result.fail("已经提交过申请，请代管理员审核完再申请");
        }
        Apply apply1 = new Apply();
        apply1.setJobId(applyVo.getJobId())
                .setType(applyVo.getType())
                .setReason(applyVo.getReason())
                .setContent(applyVo.getContent());
        int insert = applyMapper.insert(apply1);
        if (insert > 0) {
            return Result.success("申请提交成功");
        } else {
            return Result.fail("申请提交失败");
        }
    }

    /**
     * @description 管理员分页获取申请列表（可以根据状态查找）
     * @author kang
     * @date 2022/7/1 上午 10:24
     */
    @Override
    public Result applyList(ApplyVo applyVo) {
        if (ObjectUtils.isEmpty(applyVo.getCurrentPage()) || ObjectUtils.isEmpty(applyVo.getSize())) {
            return Result.fail("参数错误");
        }
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(applyVo.getStatus()) && applyVo.getStatus() == 0) {
            queryWrapper.eq(Apply::getStatus, applyVo.getStatus());
        } else {
            queryWrapper.ne(Apply::getStatus, 0);
        }
        //根据ID排序
        queryWrapper.orderByDesc(Apply::getId);
        //类型
        queryWrapper.eq(Apply::getType, applyVo.getType());
        ArrayList<ApplyDto> applyDtos = new ArrayList<>();
        Page<Apply> applyPage = applyMapper.selectPage(new Page<>(applyVo.getCurrentPage(), applyVo.getSize()), queryWrapper);
        for (Apply record : applyPage.getRecords()) {
            //查询申请人的名字
            Staff staff = staffMapper.selectById(record.getJobId());
            LambdaQueryWrapper<Audit> auditLambdaQueryWrapper = new LambdaQueryWrapper<>();
            auditLambdaQueryWrapper.eq(Audit::getApplyId, record.getId());
            Audit audit = auditMapper.selectOne(auditLambdaQueryWrapper);
            ApplyDto applyDto = new ApplyDto();

            //如果是申请职位
            if (record.getType() == 1){
                Position position = positionMapper.selectById(record.getContent());
                applyDto.setPositionName(position.getName());
            }

            //查询处理人的名字
            if (!ObjectUtils.isNull(audit)) {
                Staff staff1 = staffMapper.selectById(audit.getDealJobId());
                applyDto.setApply(record)
                        .setAudit(audit)
                        .setApplyName(staff.getName())
                        .setDealName(staff1.getName());
            } else {
                applyDto.setApply(record)
                        .setAudit(audit)
                        .setApplyName(staff.getName());
            }
            applyDtos.add(applyDto);
        }
        //分页
        Page<ApplyDto> applyDtoPage = new Page<>();
        applyDtoPage.setRecords(applyDtos)
                .setSize(applyPage.getSize())
                .setCurrent(applyPage.getCurrent())
                .setTotal(applyPage.getTotal())
                .setPages(applyPage.getPages());

        return Result.success(applyDtoPage, "管理员分页展示待审核申请列表成功");
    }

    @Override
    public Result userApplyList(ApplyVo applyVo) {
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Apply::getJobId, applyVo.getJobId())
                .orderByDesc(Apply::getId)
                .eq(Apply::getType,applyVo.getType());
        if (!ObjectUtils.isEmpty(applyVo.getStatus())){
            queryWrapper.eq(Apply::getStatus, applyVo.getStatus());
        }
        Page<Apply> applyPage = applyMapper.selectPage(new Page<Apply>(applyVo.getCurrentPage(), applyVo.getSize()), queryWrapper);
        ArrayList<ApplyDto> applyDtos = new ArrayList<>();
        List<Apply> applies = applyPage.getRecords();
        for (Apply apply : applies) {
            ApplyDto applyDto = new ApplyDto();
            Staff staff = staffMapper.selectById(apply.getJobId());
            applyDto.setApply(apply).setApplyName(staff.getName());
            if (apply.getStatus() != 0){
                //待审核
                LambdaQueryWrapper<Audit> auditLambdaQueryWrapper = new LambdaQueryWrapper<>();
                auditLambdaQueryWrapper.eq(Audit::getApplyId, apply.getId());
                Audit audit = auditMapper.selectOne(auditLambdaQueryWrapper);
                Staff staff1 = staffMapper.selectById(audit.getDealJobId());
                applyDto.setDealName(staff1.getName()).setAudit(audit);
            }
            applyDtos.add(applyDto);
        }
        //分页
        Page<ApplyDto> applyDtoPage = new Page<>();
        applyDtoPage.setRecords(applyDtos)
                .setSize(applyPage.getSize())
                .setCurrent(applyPage.getCurrent())
                .setTotal(applyPage.getTotal())
                .setPages(applyPage.getPages());
        return Result.success(applyDtoPage, "用户申请展示成功");
    }
}




